package mi.payment.settings;

import android.text.TextUtils;

import mi.payment.utils.SharedPreferencesUtil;

import static mi.payment.settings.TerminalKeyManagementBean.TERMINAL_KEY;
import static mi.payment.settings.TerminalKeyManagementBean.THREE_DES;

/**
 * @author Created by 47673 on 2017/1/9.
 */
public class TerminalKeyDataOperations {

    private static TerminalKeyDataOperations instance;
    private TerminalKeyManagementBean terminalKeyManagementBean;

    private TerminalKeyDataOperations() {
        terminalKeyManagementBean = getTerminalKeyManagementBean();
        if (terminalKeyManagementBean == null) {
            terminalKeyManagementBean = new TerminalKeyManagementBean();
            saveData(terminalKeyManagementBean);
        }
    }

    public static TerminalKeyDataOperations getInstance() {
        if (instance == null) {
            instance = new TerminalKeyDataOperations();
        }
        return instance;
    }

    public TerminalKeyManagementBean getTerminalKeyManagementBean() {
        if(terminalKeyManagementBean == null) {
            Object obj = SharedPreferencesUtil.readObj(TERMINAL_KEY);
            if(obj instanceof TerminalKeyManagementBean) {
                terminalKeyManagementBean = (TerminalKeyManagementBean) obj;
            } else {
                terminalKeyManagementBean = new TerminalKeyManagementBean();
                saveData(terminalKeyManagementBean);
            }
        }
        return terminalKeyManagementBean;
    }

    public void init(String terialKeyIndex, String KEK, String des, int icImportKey) {
        TerminalKeyManagementBean bean = getTerminalKeyManagementBean();
        bean.setTerminalKeyIndex(terialKeyIndex);
        bean.setKEK(KEK);
        bean.setDes(des);
        bean.setIcImportKey(icImportKey);
        saveData(bean);
    }

    /**
     * 清除
     */
    public void clearTerminalKeyManagementBean() {
        SharedPreferencesUtil.clearByKey(TERMINAL_KEY);
    }

    /**
     * 获取终端密钥索引
     *
     * @return 获取到的终端密钥索引
     */
    public String getTerminalKeyIndex() {
        TerminalKeyManagementBean bean = getTerminalKeyManagementBean();
        if (TextUtils.isEmpty(bean.getTerminalKeyIndex())) {
            return "0";
        } else {
            return bean.getTerminalKeyIndex();
        }
    }

    /**
     * 设置终端密钥索引
     *
     * @param terminalKeyIndex 设置的终端密钥索引值
     */
    public void saveTerminalKeyIndex(String terminalKeyIndex) {
        TerminalKeyManagementBean bean = getTerminalKeyManagementBean();
        bean.setTerminalKeyIndex(terminalKeyIndex);
        saveData(bean);
    }

    /**
     * 获取KEK
     *
     * @return 获取到的主密钥
     */
    public String getKEK() {
        TerminalKeyManagementBean bean = getTerminalKeyManagementBean();
        if (TextUtils.isEmpty(bean.getKEK())) {
            return "";
        } else {
            return bean.getKEK();
        }
    }

    /**
     * 设置主密钥
     *
     * @param masterKey 要设置的主密钥值
     */
    public void saveKEK(String masterKey) {
        TerminalKeyManagementBean bean = getTerminalKeyManagementBean();
        bean.setKEK(masterKey);
        saveData(bean);
    }

    /**
     * 获取DES算法
     *
     * @return 获取到的DES算法
     */
    public String getDes() {
        TerminalKeyManagementBean bean = getTerminalKeyManagementBean();
        if (TextUtils.isEmpty(bean.getDes())) {
            return THREE_DES;
        } else {
            return bean.getDes();
        }
    }

    /**
     * 是否3DES
     */
    public boolean is3Des() {
        return getDes().equals(TerminalKeyManagementBean.THREE_DES);
    }

    /**
     * 设置DES算法
     *
     * @param des 要设置的DES算法的值
     */
    public void saveDes(String des) {
        TerminalKeyManagementBean bean = getTerminalKeyManagementBean();
        bean.setDes(des);
        saveData(bean);
    }

    public void saveIcImportKey(int icImportKey) {
        TerminalKeyManagementBean bean = getTerminalKeyManagementBean();
        bean.setIcImportKey(icImportKey);
        saveData(bean);
    }

    public void saveData(TerminalKeyManagementBean terminalKeyManagementBean) {
        this.terminalKeyManagementBean = terminalKeyManagementBean;
        SharedPreferencesUtil.saveObj(terminalKeyManagementBean, TERMINAL_KEY);
    }

    public boolean isHasDownloadTMK() {
        TerminalKeyManagementBean bean = getTerminalKeyManagementBean();
        return bean.isHasDownloadTMK();
    }

    public void saveHasDownloadTMK(boolean hasDownloadTMK) {
        TerminalKeyManagementBean bean = getTerminalKeyManagementBean();
        bean.setHasDownloadTMK(hasDownloadTMK);
        saveData(bean);
    }
}
